package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirPortMapper;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.service.IAirPortService;

import java.util.Date;
import java.util.List;

@Service
public class AirPortServiceImpl implements IAirPortService {
    @Autowired
    private AirPortMapper airPortMapper;

    @Override
    public AirPort add(AirPort airPort) {
       ValidAirPort(airPort);
       String TimeZone=airPort.getTimezone();
       if (StrUtil.isBlank(TimeZone)){
           throw new RuntimeException("时区不能为空");
       }
       airPort.setCreateTime(new Date());
       airPort.setUpdateTime(new Date());
        int insert=airPortMapper.insert(airPort);
       if (insert>0){
           return airPort;
       }
        throw new RuntimeException("添加失败："+insert);
    }

    private void ValidAirPort(AirPort airPort){

        String AirName=airPort.getName();
        if (StrUtil.isBlank(AirName) || AirName.length()>10){
            throw new RuntimeException("机场名不能为空且不能超过十个字");
        }
        List<AirPort>list= airPortMapper.selectList(null);
        String code = airPort.getIataCode();
        String code1 = airPort.getIcaoCode();
        for (AirPort eixAir:list){
            if (code.equals(eixAir.getIataCode())  && code1.equals(eixAir.getIcaoCode())){
                throw new RuntimeException("Iate和Icao码不能重复");
            }
        }

    }

    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)){
            throw new RuntimeException("id为空");
        }
        int delete=airPortMapper.deleteById(id);
        if (delete>0) {
        return id;
        }
        throw new RuntimeException("删除失败:"+id);
    }

    @Override
    public AirPort edit(AirPort edit) {
        ValidAirPort(edit);
        String AirId=edit.getId();
        if ((StrUtil.isBlank(AirId))){
            throw new RuntimeException("id不能为空");
        }
        edit.setUpdateTime(new Date());
        int editAir = airPortMapper.updateById(edit);
        if (editAir > 0) {
            return edit;
        }
        throw new RuntimeException("修改失败:"+edit);
    }

    @Override
    public AirPort findAirPort(String name) {
        if (StrUtil.isBlank(name)){
            throw new RuntimeException("机场名字不能为空");
        }
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        AirPort airPort = airPortMapper.selectOne(queryWrapper); // 查询数据库
        if (airPort == null) {
            throw new RuntimeException("未找到名为'" + name + "'的机场");
        }
        return airPort;
    }


    @Override
    public PageVo<AirPort> pageVo(Integer page, Integer pageSize
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
//        if (StrUtil.isNotBlank(name)) {
//            // 模糊查询用户名
//            queryWrapper.like("name", name);
//        }
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<AirPort> pageParam = new Page<>(page, pageSize);
        Page<AirPort> userPage = airPortMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userPage);

    }
}
